package stu.khy.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import stu.khy.entity.Employee;
import stu.khy.entity.Leave;



public class LeaveServiceImplFM extends BaseLeaveServiceImpl {

	@Override
	protected void buildBaseHql(Employee emp, Leave leave, Date startDate,
			Date endDate, StringBuilder hqlBuilder, List<Object> values) {
		hqlBuilder
				.append("select l from Leave l, Employee e where l.creator.sn = e.sn and e.sysDepartment.id = ?");
		values.add(emp.getSysDepartment().getId());
		
		if (leave.getStatus().length() != 0) {
			hqlBuilder.append(" and l.status = ?");
			values.add(leave.getStatus());
		}
		if (startDate != null) {
			hqlBuilder.append(" and l.createTime >= ?");// yyyy-MM-dd 00:00:00
			values.add(startDate);
		}
		if (endDate != null) {
			hqlBuilder.append(" and l.createTime < ?");// yyyy-MM-dd 00:00:00
			Calendar oneDayLater = Calendar.getInstance();
			oneDayLater.setTime(endDate);
			oneDayLater.add(Calendar.DAY_OF_MONTH, 1);
			values.add(oneDayLater.getTime());
		}
	}

	@Override
	protected void addOrders(Leave leave, StringBuilder hqlBuilder) {
		if (leave.getStatus().length() == 0) {
			hqlBuilder.append(" order by l.status, l.createTime desc");
		} else {
			hqlBuilder.append(" order by l.createTime desc");
		}
	}
	
	@Override
	public void checkLeave(Leave leave) {
		Leave toCheck = getLeaveDao().get(leave.getId());
		toCheck.setStatus(leave.getStatus());
		toCheck.setApproveOpinion(leave.getApproveOpinion());
		toCheck.setModifyTime(new Date());
		if (toCheck.getStatus().equals(Constants.LEAVESTATUS_APPROVED)) {
			toCheck.setNextDeal(null);
		} else {
			toCheck.setNextDeal(toCheck.getCreator());
		}
	}

}
